//
//  CTabBar.m
//  CWeekly
//
//  Created by  on 12-4-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CTabBar.h"
@implementation CTabBar
@synthesize delegate,barButtonItems;
-(void)dealloc{
    [barButtonItems release];
    [barItems release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSString*path=[NSString bundlePath:@"tabBarItem.plist"];
        self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bottombar.png"]];

        NSArray* items=[NSArray arrayWithContentsOfFile:path];
        barItems=[[NSMutableArray alloc] init];
        UIButton* tabBarItem;
        barButtonItems=[[NSMutableArray alloc] init];
        [self addSpaceItemCount:3];
        for (int i=0; i<items.count; i++) {
            NSDictionary* item=[items objectAtIndex:i];
            UIImage*img=[UIImage imageNamed:[item valueForKey:@"icon"]];
            UIImage* simg=[UIImage imageNamed:[item valueForKey:@"sIcon"]];
            tabBarItem=[UIButton buttonWithType:UIButtonTypeCustom];
            tabBarItem.frame=CGRectMake(0, 0, 40, 55);
            [tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            tabBarItem.tag=i+1000;
            [tabBarItem setImage:img forState:UIControlStateNormal];
            [tabBarItem setImage:simg forState:UIControlStateSelected];
           UIBarButtonItem* barBTn=[[UIBarButtonItem alloc] initWithCustomView:tabBarItem];
            [barItems addObject:barBTn];
            [barButtonItems addObject:barBTn];
            [barBTn release];
            [self addSpaceItemCount:1];
            UIImageView*imgv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vertical_line.png"]];
            UIBarButtonItem* lineItem=[[UIBarButtonItem alloc] initWithCustomView:imgv];
            [barItems addObject:lineItem];
            [imgv release];
            [lineItem release];
            [self addSpaceItemCount:1];

        }
        [barItems removeLastObject];
        [barItems removeLastObject];
        [self addSpaceItemCount:3];

        self.items=barItems;
    }
    return self;
}
-(void)addSpaceItemCount:(NSUInteger)count{
    UIBarButtonItem*  spaceBTN=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    for (int i=0; i<count; i++) {
        [barItems addObject:spaceBTN];
    }
    [spaceBTN release];

}
-(void)itemSelected:(CTabBarItem*)sender{
    for (UIBarButtonItem*item in barItems) {
        CTabBarItem* btn=(CTabBarItem*) item.customView;
        if (btn.tag>999) {            
            if (btn.tag==sender.tag) {
                btn.selected=YES;
            }else {
                btn.selected=NO;
            }
        }
    }
    [delegate barSelectAtIndex:sender.tag];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
//    UIImage* img;
//    if (rect.size.width>1000) {
//        img=[UIImage imageNamed:@"bottombar.png"];
//    }else {
//        img=[UIImage imageNamed:@"bottombar_v.png"];
//    }
//    [img drawInRect:rect];
}


@end
